﻿using System;
using UnityEngine;

namespace RenderHeads.Media.AVProVideo.Demos
{
	// Token: 0x0200000F RID: 15
	public class PlaybackSync : MonoBehaviour
	{
		// Token: 0x06000045 RID: 69 RVA: 0x00003CEC File Offset: 0x00001EEC
		private void Start()
		{
			for (int i = 0; i < this._slavePlayers.Length; i++)
			{
				this._slavePlayers[i].m_Muted = true;
				if (this._matchVideo)
				{
					this._slavePlayers[i].OpenVideoFromFile(this._masterPlayer.m_VideoLocation, this._masterPlayer.m_VideoPath, false);
				}
			}
		}

		// Token: 0x06000046 RID: 70 RVA: 0x00003D50 File Offset: 0x00001F50
		private void LateUpdate()
		{
			if (this._state == PlaybackSync.State.Loading && this.IsAllVideosLoaded())
			{
				this._masterPlayer.Play();
				for (int i = 0; i < this._slavePlayers.Length; i++)
				{
					this._slavePlayers[i].Play();
				}
				this._state = PlaybackSync.State.Playing;
			}
			if (this._state == PlaybackSync.State.Finished)
			{
				GlobalData.Debug_Log("Do Something");
			}
			else if (this._state == PlaybackSync.State.Playing)
			{
				if (this._masterPlayer.Control.IsPlaying())
				{
					float currentTimeMs = this._masterPlayer.Control.GetCurrentTimeMs();
					for (int j = 0; j < this._slavePlayers.Length; j++)
					{
						MediaPlayer mediaPlayer = this._slavePlayers[j];
						float currentTimeMs2 = mediaPlayer.Control.GetCurrentTimeMs();
						float num = Mathf.Abs(currentTimeMs - currentTimeMs2);
						if (num > this._toleranceMs)
						{
							mediaPlayer.Control.SeekFast(currentTimeMs + this._toleranceMs * 0.5f);
							if (mediaPlayer.Control.IsPaused())
							{
								mediaPlayer.Play();
							}
						}
					}
				}
				else
				{
					for (int k = 0; k < this._slavePlayers.Length; k++)
					{
						MediaPlayer mediaPlayer2 = this._slavePlayers[k];
						mediaPlayer2.Pause();
					}
				}
				if (PlaybackSync.IsPlaybackFinished(this._masterPlayer))
				{
					this._state = PlaybackSync.State.Finished;
				}
			}
		}

		// Token: 0x06000047 RID: 71 RVA: 0x00003EBC File Offset: 0x000020BC
		private bool IsAllVideosLoaded()
		{
			bool result = false;
			if (PlaybackSync.IsVideoLoaded(this._masterPlayer))
			{
				result = true;
				for (int i = 0; i < this._slavePlayers.Length; i++)
				{
					if (!PlaybackSync.IsVideoLoaded(this._slavePlayers[i]))
					{
						result = false;
						break;
					}
				}
			}
			return result;
		}

		// Token: 0x06000048 RID: 72 RVA: 0x00003F10 File Offset: 0x00002110
		private static bool IsVideoLoaded(MediaPlayer player)
		{
			return player != null && player.Control != null && player.Control.HasMetaData() && player.Control.CanPlay() && player.TextureProducer.GetTextureFrameCount() > 0;
		}

		// Token: 0x06000049 RID: 73 RVA: 0x00003F68 File Offset: 0x00002168
		private static bool IsPlaybackFinished(MediaPlayer player)
		{
			bool result = false;
			if (player != null && player.Control != null && player.Control.IsFinished())
			{
				result = true;
			}
			return result;
		}

		// Token: 0x04000035 RID: 53
		public MediaPlayer _masterPlayer;

		// Token: 0x04000036 RID: 54
		public MediaPlayer[] _slavePlayers;

		// Token: 0x04000037 RID: 55
		public float _toleranceMs = 30f;

		// Token: 0x04000038 RID: 56
		public bool _matchVideo = true;

		// Token: 0x04000039 RID: 57
		public bool _muteSlaves = true;

		// Token: 0x0400003A RID: 58
		private PlaybackSync.State _state;

		// Token: 0x02000010 RID: 16
		private enum State
		{
			// Token: 0x0400003C RID: 60
			Loading,
			// Token: 0x0400003D RID: 61
			Playing,
			// Token: 0x0400003E RID: 62
			Finished
		}
	}
}
